const app = getApp();
import {
   getUserInfo
} from '../../api/user.js';
import {
   team
} from '../../api/store.js';
Page({

   /**
    * 页面的初始数据
    */
   data: {
      parameter: {
         'navbar': '1',
         'return': '1',
         'title': '我的团长'
      },
      shopList: [],
      loading: false,
      loadend: false,
      loadTitle: '加载更多',
      currentData: 1,
      profitsort: 0,
      relaysort: 0,
      nows: true,
      news: 0,
      where: {
         profitOrder: '',
         shareCountOrder: '',
         page: 1,
         limit: 999
      },
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.getUserInfo();
      this.team();
   },
   /**
    * 获取个人用户信息
    */
   getUserInfo: function() {
      var that = this;
      getUserInfo().then(res => {
         that.setData({
            userInfo: res.data
         });
      });
   },
   set_sort: function(e) {
      var dataset = e.target.dataset;
      var page = this.data.page;
      switch (dataset.type) {
         case '1':
            this.setData({
               profitsort: 0,
               relaysort: 0,
               nows: true
            });
            break;
         case '2':
            if (this.data.profitsort == 0)
               this.data.profitsort = 1;
            else if (this.data.profitsort == 1)
               this.data.profitsort = 2;
            else if (this.data.profitsort == 2)
               this.data.profitsort = 0;
            this.setData({
               profitsort: this.data.profitsort,
               nows: false,
               relaysort: 0
            });
            break;
         case '3':
            if (this.data.relaysort == 0)
               this.data.relaysort = 1;
            else if (this.data.relaysort == 1)
               this.data.relaysort = 2;
            else if (this.data.relaysort == 2)
               this.data.relaysort = 0;
            this.setData({
               relaysort: this.data.relaysort,
               nows: false,
               profitsort: 0
            });
            break;
      }
      this.setData({
         loadend: false,
         ['where.page']: 1
      });
      this.team(true);
   },
   //设置where条件
   setWhere: function() {
      if (this.data.profitsort == 0)
         this.data.where.profitOrder = '';
      else if (this.data.profitsort == 1)
         this.data.where.profitOrder = 'desc';
      else if (this.data.profitsort == 2)
         this.data.where.profitOrder = 'asc';
      if (this.data.relaysort == 0)
         this.data.where.shareCountOrder = '';
      else if (this.data.relaysort == 1)
         this.data.where.shareCountOrder = 'desc';
      else if (this.data.relaysort == 2)
         this.data.where.shareCountOrder = 'asc';
      this.data.news = this.data.nows ? 1 : 0;
      this.setData({
         where: this.data.where
      });
   },
   team: function(isPage) {
      var that = this
      this.setWhere();
      if (that.data.loading) return;
      if (that.data.loadend) return;
      if (isPage === true) that.setData({
         shopList: []
      });
      that.setData({
         loading: true,
         loadTitle: ''
      });
      team(that.data.where).then(res => {
         var list = res.data.list;
         let shopList = app.SplitArray(list, that.data.shopList);
         let loadend = list.length < that.data.where.limit;
         this.setData({
            list: shopList,
            loadend: loadend,
            loadTitle: loadend ? '我也是有底线的' : '加载更多',
            ['where.page']: that.data.where.page + 1,
            loading: false,
            profit: res.data.profit,
            shareCount: res.data.shareCount
         })
      }).catch(err => {
         that.setData({
            loading: false,
            loadTitle: '加载更多'
         });
      });
   },
   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {
      this.team();
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   }
})